﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Mac;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using CPFUI.IControl.KeyBord;
using CPFUI.IHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class KeyBorderNumber : Control
    {
        
        StackPanel pnlCharContent;
        bool isupper = false;
        bool isNormal = true;
        List<Keys> pressedModifierKeyCodes = null;

        /// <summary>
        /// 按下的组合键集合
        /// </summary>
        List<Keys> PressedModifierKeyCodes
        {
            get
            {
                if (pressedModifierKeyCodes == null)
                {
                    pressedModifierKeyCodes = new List<Keys>();
                }
                return pressedModifierKeyCodes;
            }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            pnlCharContent = new StackPanel() { 
                MarginLeft=0,
                MarginBottom=0,
                MarginTop=0,
                MarginRight=0
            };
            
            Children.Add(pnlCharContent);
            pnlCharContent.Children.Add(new StackPanel
            {
                Height=65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.Divide,"/","")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=132,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Multiply,"*","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Subtract,"-","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.NumPad7,"7","")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.NumPad8,"8","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.NumPad9,"9","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Add,"+","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=132,
                    },
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.NumPad4,"4","")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.NumPad5,"5","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.NumPad6,"6","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.NumPad1,"1","")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.NumPad2,"2","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                        UnNumLockKeyCode=40,
                    },
                    new KeyBorderItem(Keys.NumPad3,"3","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Enter,"Enter","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=132,
                    }
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.NumPad0,"0","")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=132,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Decimal,".","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                }
            });


        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            for (int i = 0; i < pnlCharContent.Children.Count; i++)
            {
                StackPanel pnl = (StackPanel)pnlCharContent.Children[i];
                if (pnl == null)
                {
                    continue;
                }
                for (int j = 0; j < pnl.Children.Count; j++)
                {
                    KeyBorderItem txtkey = pnl.Children[j] as KeyBorderItem;
                    if (txtkey == null)
                    {
                        continue;
                    }
                    txtkey.MouseDown += KeyBorder_MouseDown;
                }
            }
            
        }

        private void KeyBorder_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            KeyBorderItem keyItem = sender as KeyBorderItem;
            if (keyItem == null)
            {
                return;
            }
            SyncKeyPairs(keyItem);
            if (ProcessSpecialKey(keyItem))
            {
                return;
            }
            if (keyItem.KeyCode == Keys.NumLock || keyItem.KeyCode == Keys.ShiftKey || keyItem.KeyCode == Keys.CapsLock)
            {
                ValueChange(false, true, false);
            }
            if (keyItem.IsLockKey)
            {
                KeyboardInput.SendToggledKey(keyItem.KeyCode);
            }
            else if (keyItem.IsModifierKey)
            {
                if (PressedModifierKeyCodes.Contains(keyItem.KeyCode))
                {
                    if (keyItem.KeyCode == Keys.ShiftKey|| keyItem.KeyCode == Keys.LWin)
                    {
                        KeyboardInput.SendToggledKey(keyItem.KeyCode);
                    }
                    PressedModifierKeyCodes.Remove(keyItem.KeyCode);
                }
                else
                {
                    PressedModifierKeyCodes.Add(keyItem.KeyCode);
                }
            }
            else if (keyItem.IsIMEKey)
            {
                IntPtr hw = UnsafeNativeMethods.GetForegroundWindow();
                //如果是中英文切换按键
                //KeyboardInput.SendToggledKey(Keys.ShiftKey);
                keyItem.NormalText= hw.ToString();
                keyItem.UpdateDisplayText(false, true, false);
                UnsafeNativeMethods.SimulateShiftKeyPress(hw);
            }
            else
            {
                Keys btnKeyCode = keyItem.KeyCode;

                //如果该键是数字键盘键且未按下NumLock，则使用
                if (keyItem.IsNumberPadKey && !(true) && keyItem.UnNumLockKeyCode > 0)
                {
                    btnKeyCode = (Keys)keyItem.UnNumLockKeyCode;
                }

                KeyboardInput.SendKey(PressedModifierKeyCodes, btnKeyCode);

                //清除所有组合键按钮的按下状态。
                ResetModifierKeyButtons();
            }
        }
        string languge = "zh-CN";
        void ResetModifierKeyButtons()
        {
            PressedModifierKeyCodes.Clear();

            ValueChange(false, true, false);
        }
        //用户代码

        /// <summary>
        /// 改变切换字符
        /// </summary>
        private void ValueChange(bool isShiftKeyPressed, bool isNumLockPressed, bool isCapsLockPressed)
        {
            for (int i = 0; i < pnlCharContent.Children.Count; i++)
            {
                StackPanel pnl = (StackPanel)pnlCharContent.Children[i] ;
                if (pnl == null)
                {
                    continue;
                }
                for (int j = 0; j < pnl.Children.Count; j++)
                {
                    KeyBorderItem keyitem = pnl.Children[j] as KeyBorderItem;
                    if (keyitem == null)
                    {
                        continue;
                    }
                    
                    keyitem.UpdateDisplayText(isShiftKeyPressed, isNumLockPressed, isCapsLockPressed);
                }
            }
        }

        void SyncKeyPairs(KeyBorderItem item)
        {
          
        }
        bool ProcessSpecialKey(KeyBorderItem btn)
        {
            bool handled = true;
            switch (btn.KeyCode)
            {

                // Use Shift+F10 to simulate the Apps key. 
                case Keys.Apps:
                    KeyboardInput.SendKey(
                                        new Keys[] { Keys.ShiftKey },
                                        Keys.F10);
                    break;
                default:
                   handled = false;
                    break;
            }
            return handled;
        }

#endif
    }
}
